package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TbAppCatalog;
import com.ruoyi.system.domain.dto.TbAppVersionCreateDTO;
import com.ruoyi.system.domain.vo.TbAppCatlogAndVersionVO;
import com.ruoyi.system.domain.vo.TbAppVersionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbAppVersionMapper;
import com.ruoyi.system.domain.TbAppVersion;
import com.ruoyi.system.service.ITbAppVersionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 应用版本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Service
public class TbAppVersionServiceImpl implements ITbAppVersionService {
    @Autowired
    private TbAppVersionMapper tbAppVersionMapper;


    /**
     * 查询应用版本信息
     *
     * @param id 应用版本信息主键
     * @return 应用版本信息
     */
    @Override
    public TbAppVersion selectTbAppVersionById(Long id) {
        return tbAppVersionMapper.selectTbAppVersionById(id);
    }

    /**
     * 查询应用版本信息列表
     *
     * @param tbAppVersion 应用版本信息
     * @return 应用版本信息
     */
    @Override
    public List<TbAppVersion> selectTbAppVersionList(TbAppVersion tbAppVersion) {
        return tbAppVersionMapper.selectTbAppVersionList(tbAppVersion);
    }

    /**
     * 新增应用版本信息
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insertTbAppVersion(TbAppVersionCreateDTO dto) {
        TbAppVersion appVersion = tbAppVersionMapper.getAppVersionByPackageVersioncode(dto.getVersionCode(), dto.getVersionName(), dto.getPackageName());
        if (appVersion != null) {
            return R.fail("该版本已存在");
        }
        //获取到包名相同的应用版本的 PackageId
        Long packageId = tbAppVersionMapper.getPackageIdByPackageName(dto.getPackageName());
        TbAppVersion tbAppVersion = new TbAppVersion();
        tbAppVersion.setPackageId(packageId);
        tbAppVersion.setPackageName(dto.getPackageName());
        tbAppVersion.setPackageVersionnumber(dto.getVersionName());
        tbAppVersion.setPackageVersioncode(dto.getVersionCode());
        tbAppVersion.setStatus(dto.getStatus());
        tbAppVersion.setPackageHash(dto.getPackageHash());
        tbAppVersion.setPackageType(1);
        tbAppVersion.setPackageSize(dto.getFileSizeMB());
        tbAppVersion.setOsRequirement(dto.getMinSystemVersion());
        tbAppVersion.setSdkRequirement(dto.getSdkRequirement());
        tbAppVersion.setIsForceUpdate(dto.getForceUpdate());
        tbAppVersion.setPublishDate(new Date());
        tbAppVersion.setPackageUrl(dto.getPackageUrl());
        tbAppVersion.setRemark(dto.getVersionDesc());
        tbAppVersion.setCreateTime(DateUtils.getNowDate());
        tbAppVersion.setCreateBy(SecurityUtils.getUsername());
        return R.ok(tbAppVersionMapper.insertTbAppVersion(tbAppVersion));
    }

    /**
     * 修改应用版本信息
     *
     * @param tbAppVersion 应用版本信息
     * @return 结果
     */
    @Override
    public int updateTbAppVersion(TbAppVersion tbAppVersion) {
        tbAppVersion.setUpdateTime(DateUtils.getNowDate());
        return tbAppVersionMapper.updateTbAppVersion(tbAppVersion);
    }

    /**
     * 批量删除应用版本信息
     *
     * @param ids 需要删除的应用版本信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAppVersionByIds(Long[] ids) {
        return tbAppVersionMapper.deleteTbAppVersionByIds(ids);
    }

    /**
     * 删除应用版本信息信息
     *
     * @param id 应用版本信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAppVersionById(Long id) {
        return tbAppVersionMapper.deleteTbAppVersionById(id);
    }

    @Override
    public List<TbAppCatlogAndVersionVO> selectTbAppCatalogAndVersionList(TbAppCatalog tbAppCatalog) {

        return tbAppVersionMapper.selectTbAppCatalogAndVersionList(tbAppCatalog);
    }

    @Override
    public TbAppVersion getAppVersionByPackageVersioncode(String versionCode, String versionName, String packageName) {
        return tbAppVersionMapper.getAppVersionByPackageVersioncode(versionCode, versionName, packageName);
    }

    @Override
    public List<TbAppVersionVO> selectTbAppVersionLis(TbAppVersion tbAppVersion) {

        return tbAppVersionMapper.selectTbAppVersionLis(tbAppVersion);
    }

    @Override
    public Long getPackageIdByPackageName(String packageName) {
        return tbAppVersionMapper.getPackageIdByPackageName(packageName);

    }
}
